<template>
	<view class="warning-settings-container">
		<!-- 预警开关 -->
		<view class="section">
			<u-cell-group>
				<u-cell title="开启用水预警">
					<template #right-icon>
						<u-switch v-model="settings.enabled" @change="handleEnableChange"></u-switch>
					</template>
				</u-cell>
			</u-cell-group>
		</view>

		<!-- 预警阈值设置 -->
		<view class="section" v-if="settings.enabled">
			<u-cell-group title="预警阈值设置">
				<u-cell title="日用水量预警">
					<template #right-icon>
						<view class="threshold-input">
							<u-input
								v-model="settings.dailyThreshold"
								type="number"
								:border="false"
							></u-input>
							<text class="unit">吨</text>
						</view>
					</template>
				</u-cell>
				<u-cell title="月用水量预警">
					<template #right-icon>
						<view class="threshold-input">
							<u-input
								v-model="settings.monthlyThreshold"
								type="number"
								:border="false"
							></u-input>
							<text class="unit">吨</text>
						</view>
					</template>
				</u-cell>
			</u-cell-group>
		</view>

		<!-- 通知方式设置 -->
		<view class="section" v-if="settings.enabled">
			<u-cell-group title="通知方式">
				<u-cell title="短信通知">
					<template #right-icon>
						<u-switch v-model="settings.smsNotification"></u-switch>
					</template>
				</u-cell>
				<u-cell title="微信通知">
					<template #right-icon>
						<u-switch v-model="settings.wechatNotification"></u-switch>
					</template>
				</u-cell>
				<u-cell title="App推送">
					<template #right-icon>
						<u-switch v-model="settings.appNotification"></u-switch>
					</template>
				</u-cell>
			</u-cell-group>
		</view>

		<!-- 通知时间设置 -->
		<view class="section" v-if="settings.enabled">
			<u-cell-group title="通知时间">
				<u-cell title="通知时段">
					<template #right-icon>
						<text class="time-range" @click="showTimePicker = true">
							{{settings.startTime}} - {{settings.endTime}}
						</text>
					</template>
				</u-cell>
			</u-cell-group>
		</view>

		<!-- 保存按钮 -->
		<view class="save-btn">
			<u-button type="primary" @click="saveSettings">保存设置</u-button>
		</view>

		<!-- 时间选择器 -->
		<u-popup :show="showTimePicker" mode="bottom" @close="showTimePicker = false">
			<view class="time-picker">
				<view class="picker-header">
					<text>选择通知时段</text>
					<u-icon name="close" @click="showTimePicker = false"></u-icon>
				</view>
				<view class="picker-content">
					<u-picker
						:show="showTimePicker"
						:columns="timeColumns"
						@confirm="confirmTime"
						@cancel="showTimePicker = false"
					></u-picker>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
export default {
	data() {
		return {
			settings: {
				enabled: true,
				dailyThreshold: '5.0',
				monthlyThreshold: '150.0',
				smsNotification: true,
				wechatNotification: true,
				appNotification: true,
				startTime: '08:00',
				endTime: '22:00'
			},
			showTimePicker: false,
			timeColumns: [
				['00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00'],
				['00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00']
			]
		}
	},
	methods: {
		handleEnableChange(value) {
			this.settings.enabled = value
		},
		confirmTime(e) {
			const [start, end] = e.value
			this.settings.startTime = this.timeColumns[0][start]
			this.settings.endTime = this.timeColumns[1][end]
			this.showTimePicker = false
		},
		saveSettings() {
			// TODO: 保存设置到后端
			uni.showToast({
				title: '设置已保存',
				icon: 'success'
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.warning-settings-container {
	min-height: 100vh;
	background-color: #f5f5f5;
	padding-bottom: 120rpx;

	.section {
		margin-bottom: 20rpx;
	}

	.threshold-input {
		display: flex;
		align-items: center;

		.unit {
			margin-left: 10rpx;
			color: #666;
		}
	}

	.time-range {
		color: #666;
	}

	.save-btn {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 20rpx;
		background-color: #fff;
		box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
	}

	.time-picker {
		.picker-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 30rpx;

			text {
				font-size: 32rpx;
				font-weight: bold;
			}
		}
	}
}
</style>
